<template>
	<view class="loveCar">
		<view class="uni-form-ocr">
		<ocr-navigator @onSuccess="driverSuccess"   certificateType="drivingLicense" selectedOptions="{{['plateNum','vehicleType','owner']}}">
		  <button type="primary">行驶证识别</button>
		</ocr-navigator> 
		</view>
		<view class="uni-form-item">
			<view class="title">
				车牌：
			</view>
			<view class="uni_name">
				<input class="uni-input" v-model="brand" placeholder="请输入车牌" />
			</view>
		</view>
		<view class="uni-form-item">

			<view class="title">车型：</view>
			<view class="uni_name">
				<input class="uni-input" v-model="carModel" placeholder="请输入车型" />
			</view>
		</view>
		<view class="uni-form-item">
			<view class="title">年份：</view>
			<view class="uni_name">
				<picker mode="date" :value="date" :start="startDate" :end="endDate" fields="year"
					@change="bindDateChange">
				<view v-if="!date">选择年份</view>	
				<view v-else>{{date}}</view>
				</picker>
			</view>
		</view>
		<view class="uni-form-item">
			<view class="title">手机号：</view>
			<view class="uni_name">
				<input class="uni-input" v-model="phoneNumber" placeholder="请输入手机号码" />
			</view>
		</view>
		<view class="uni-form-item">
			<view class="title">发动机号：</view>
			<view class="uni_name">
				<input class="uni-input" v-model="engineNumber" placeholder="请输入发动机号" />
			</view>
		</view>

		<view class="uni-form-item">
			<view class="title">绑定vin:</view>
			<view class="uni_name">
				<input class="uni-input" v-model="vin" placeholder="请输入vin" />
			</view>
		</view>
		<view class="uni-form-item search" style="background:#07c160;height:80rpx;line-height: 80rpx;" @click="openSearch">
			确    认
		</view>

	</view>

</template>

<script setup>
	import {
		onMounted,
		ref,
		defineProps,
		computed,
		watch,
		reactive
	} from 'vue'
	import {
		RSAencrypt,
		RSAdecrypt
	} from "../../utils/rsa.js"
	import {getMyCar} from "../../api/health.js"
	const inputValue = ref('')
	const date = ref('')
	const startDate = ref('1980-01-01')
	const endtDate = ref('2024-12-30')
	
	
	const brand = ref('')
	const carModel = ref('')
	const phoneNumber = ref('')
	const engineNumber = ref('')
	const vin = ref('')
	
	
	onMounted(()=>{
		// getInfo()
	})
	const driverSuccess = (item) => {
		console.log('行驶证识别结果',item.detail)
		brand.value = item.detail.plate_num.text 
		carModel.value = item.detail.model.text  
		engineNumber.value = item.detail.engine_num.text
		vin.value = item.detail.vin.text
		date.value = item.detail.register_date.text.substring(0,4)
	}
	
	
	const bindDateChange = (e) => {
		date.value = e.target.value;
	}
	
	const getInfo = () => {
	
		// console.log(uni.getStorageSync('openid'), '加密前的openid')
		// let deOpenId = RSAencrypt(uni.getStorageSync('openid'))
		// // let dcOpenId = RSAdecrypt(deOpenId)
	
	
		console.log(uni.getStorageSync('openid'),'加密前的openid')
		let deOpenId = RSAencrypt(uni.getStorageSync('openid'))
		// let dcOpenId = RSAdecrypt(deOpenId)
		
		uni.request({
			url: 'https://www.chargesystem.cn/charge4gv2/wechat/getUserInfo', // 你的登录API地址
			method: 'GET',
			data: {
				openid: deOpenId,
			},
			success: (res) => {
				if(res.data.code===200&&res.data.result.myCar){
					vin.value = res.data.result.myCar.vin?res.data.result.myCar.vin:'';
					date.value = res.data.result.myCar.carYear?res.data.result.myCar.carYear:'';
					carModel.value =res.data.result.myCar.inputCarModel?res.data.result.myCar.inputCarModel:'';
					engineNumber.value = res.data.result.myCar.engineNumber?res.data.result.myCar.engineNumber:'';
					brand.value = res.data.result.myCar.licensePlate?res.data.result.myCar.licensePlate:'';
					phoneNumber.value = res.data.result.myCar.phoneNumber?res.data.result.myCar.phoneNumber:'';
				}
				console.log(res,'resssssssssssss')
			},
			fail: () => {
				uni.showToast({
					title: '请求失败',
					icon: 'none'
				});
			}
		});
	}
	
	// http://101.43.94.70:25500
	const openSearch = () => {
		console.log(brand.value,carModel.value,phoneNumber.value,engineNumber.value,vin.value,date.value,'ffffffffffff')
		
		
		
		let deOpenId = RSAencrypt(uni.getStorageSync('openid'))
		let deVin = RSAencrypt(inputValue.value)
		console.log(uni.getStorageSync('openid'), uni.getStorageSync('sessionKey'))
		
		// '"carYear": "2013'"engineNumber":"999888777"
		// inputCarModel":"奔驰小G""licensePlate":"京A00001"
		// "openId":"o59y]7RVxi67Et8naj0lqoLLgx0s"
		// "phoneNumber":"15866998877""vin":"LCOCE6DB4R4247059
		// '
		uni.request({
			url: 'https://www.chargesystem.cn/charge4gv2/wechat/addMyCar', // 你的登录API地址
			method: 'POST',
			data: {
				carYear:RSAencrypt(date.value),
				engineNumber:RSAencrypt(engineNumber.value),
				inputCarModel:RSAencrypt(carModel.value),
				licensePlate:RSAencrypt(brand.value),
				openId:RSAencrypt(uni.getStorageSync('openid')),
				phoneNumber:RSAencrypt(phoneNumber.value),
				vin:RSAencrypt(vin.value)
				// licensePlate:RSAencrypt(brand),
				// inputCarModel:RSAencrypt(carModel),
				// engineNumber:RSAencrypt(engineNumber),
				// vin:RSAencrypt(vin),
				// carYear:RSAencrypt(date),
				// phoneNumber:RSAencrypt(phoneNumber),
				//  openid: deOpenId, // uni.getStorageSync('openid'),
				// vin: deVin
			},
			success: (res) => {
				console.log(res, 'res')
				if (res.data.code == 200) {
					uni.showToast({
						title: '绑定成功！',
						icon: 'none'
					});
					uni.reLaunch({
						url: '/pages/index/index'
					})
				}
				//           if (res.data &&res.data.code ===200) {
				//             const { openId, sessionKey } = res.data.result;
				// uni.setStorageSync('openId', openId);
				// uni.setStorageSync('sessionKey', sessionKey);
				// uni.setStorageSync('isLogin', true);



				// // 跳转页面 回首页

				// uni.reLaunch({
				// 	url:'/pages/index/index'
				// })

				//             // 将用户信息和session存储到本地
				// console.log(openId, sessionKey, unionId,'openId, sessionKey, unionId')

				//           } else {
				//             uni.showToast({
				//               title: '登录失败',
				//               icon: 'none'
				//             });
				//           }
			},
			fail: () => {
				uni.showToast({
					title: '请求失败',
					icon: 'none'
				});
			}
		});


	}

	// const onInputBrand = (e) => {
	// 	onInputBrand().value = e.target.value
	// }
	// const onInput = (e) => {
	// 	inputValue.value = e.target.value
	// 	console.log('输入的内容为：', e.target.value);
	// }
</script>
<style lang="scss" scoped>
	.uni-form-ocr{
		display: flex;
		justify-content: center;
		padding: 40rpx;
		background-color: #fff;
		margin-bottom: 20rpx;
	}
	.picker {
		padding: 10px;
		background-color: #fff;
		text-align: center;
	}

	.uni_name {
		display: flex;
		align-items: center;
	}



	.loveCar {
		// padding: 20rpx;
		background-color: #f2f2f2;
		min-height: 1200rpx;

		.uni-form-item {

			display: flex;
			// align-items: center;
			background-color: #fff;
			margin-bottom: 20rpx;
			padding-left: 20rpx;
			line-height: 100rpx;
			height: 100rpx;
			color: #000;
			font-size: 32rpx;
			font-weight: 500;

			.title {
				margin-right: 20rpx;
			}

		}

		.uni-label {
			width: 80%;
			font-size: 26rpx;
			margin-bottom: 20rpx;
		}

		.uni-input {
			// margin: 0 20rpx;
			width: 500rpx;
			border-bottom: solid 2rpx #ccc;
		}

		.search {
			margin: 0 auto;
			width: 300rpx;
			height: 60rpx;
			background-color: #0076ff;
			border-radius: 10rpx;
			text-align: center;
			line-height: 60rpx;
			color: #fff;

			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>